<!DOCTYPE html>
<html lang="zh-cn">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">
  <link rel="icon" type="image/png" sizes="16x16" href="https://gitee.com/reku1997/reku1997/raw/master/reku.ico">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"reku1997.gitee.io","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":"flat"},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"appID":"AW5K8S9IEE","apiKey":"d7e666d597854738d2fb31ecaa989aa5","indexName":"dev_reku1997","hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="幸好没参加，参加了就得回到Div 2。">
<meta property="og:type" content="article">
<meta property="og:title" content="Codeforces AIM Tech Round 3 (Div. 1) 题解">
<meta property="og:url" content="https://reku1997.gitee.io/2016/08/26/codeforces-aim-tech-round-3-div-1/index.html">
<meta property="og:site_name" content="Reku">
<meta property="og:description" content="幸好没参加，参加了就得回到Div 2。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://www.reku.me/wp-content/uploads/2016/08/捕获.png">
<meta property="article:published_time" content="2016-08-26T09:09:43.000Z">
<meta property="article:modified_time" content="2021-12-16T12:08:55.000Z">
<meta property="article:author" content="Reku">
<meta property="article:tag" content="网络流">
<meta property="article:tag" content="codeforces">
<meta property="article:tag" content="最小费用最大流">
<meta property="article:tag" content="构造">
<meta property="article:tag" content="树DP">
<meta property="article:tag" content="贪心">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://www.reku.me/wp-content/uploads/2016/08/捕获.png">

<link rel="canonical" href="https://reku1997.gitee.io/2016/08/26/codeforces-aim-tech-round-3-div-1/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-cn'
  };
</script>

  <title>Codeforces AIM Tech Round 3 (Div. 1) 题解 | Reku</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="Reku" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">Reku</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-fw fa-home"></i>Home</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-fw fa-user"></i>About</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>Tags</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>Archives</a>

  </li>
        <li class="menu-item menu-item-sitemap">

    <a href="/sitemap.xml" rel="section"><i class="fa fa-fw fa-sitemap"></i>Sitemap</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>Search
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container"></div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="algolia-results">
  <div id="algolia-stats"></div>
  <div id="algolia-hits"></div>
  <div id="algolia-pagination" class="algolia-pagination"></div>
</div>

      
    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-cn">
    <link itemprop="mainEntityOfPage" href="https://reku1997.gitee.io/2016/08/26/codeforces-aim-tech-round-3-div-1/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="Reku">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Reku">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Codeforces AIM Tech Round 3 (Div. 1) 题解
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">Posted on</span>

              <time title="Created: 2016-08-26 17:09:43" itemprop="dateCreated datePublished" datetime="2016-08-26T17:09:43+08:00">2016-08-26</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">Edited on</span>
                <time title="Modified: 2021-12-16 20:08:55" itemprop="dateModified" datetime="2021-12-16T20:08:55+08:00">2021-12-16</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">In</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/acm/" itemprop="url" rel="index"><span itemprop="name">acm</span></a>
                </span>
            </span>

          
            <span id="/2016/08/26/codeforces-aim-tech-round-3-div-1/" class="post-meta-item leancloud_visitors" data-flag-title="Codeforces AIM Tech Round 3 (Div. 1) 题解" title="Views">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">Views: </span>
              <span class="leancloud-visitors-count"></span>
            </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="fa fa-comment-o"></i>
      </span>
      <span class="post-meta-item-text">Valine: </span>
    
    <a title="valine" href="/2016/08/26/codeforces-aim-tech-round-3-div-1/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2016/08/26/codeforces-aim-tech-round-3-div-1/" itemprop="commentCount"></span>
    </a>
  </span>
  
  

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>幸好没参加，参加了就得回到Div 2。 <span id="more"></span> <a target="_blank" rel="noopener" href="http://codeforces.com/contest/708/problem/A">http://codeforces.com/contest/708/problem/A</a></p>
<blockquote>
<p>题目大意： 给定一个字符串，选择一个非空子串进行前移操作(<span class="math inline">\(...b-&gt;a-&gt;z\)</span>)，求经过操作后字典序最小的子串</p>
</blockquote>
<p>傻吊题，找到第一个两个<span class="math inline">\(a\)</span>之间串进行操作就可以了，注意<span class="math inline">\(aa...a\)</span>要特判就行了。</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;bits/stdc++.h&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line">string s;</span><br><span class="line"><span class="keyword">int</span> bj=<span class="number">0</span>;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	cin&gt;&gt;s;</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;s.<span class="built_in">size</span>();i++)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">if</span>(s[i]==<span class="string">&#x27;a&#x27;</span>&amp;&amp;bj==<span class="number">1</span>) <span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">if</span>(s[i]!=<span class="string">&#x27;a&#x27;</span>)</span><br><span class="line">		&#123;</span><br><span class="line">			bj=<span class="number">1</span>;</span><br><span class="line">			s[i]--;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">if</span>(bj==<span class="number">0</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		s[s.<span class="built_in">size</span>()<span class="number">-1</span>]=<span class="string">&#x27;z&#x27;</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	cout&lt;&lt;s;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><a target="_blank" rel="noopener" href="http://codeforces.com/contest/708/problem/B">http://codeforces.com/contest/708/problem/B</a></p>
<blockquote>
<p>题目大意： 给出四个数字，代表<span class="math inline">\(00\)</span>子序列的个数，<span class="math inline">\(01\)</span>子序列的个数，<span class="math inline">\(10\)</span>子序列的个数，<span class="math inline">\(11\)</span>子序列的个数，构造一个符合条件的<span class="math inline">\(01\)</span>串</p>
</blockquote>
<p>其实也挺脑残的，就是有很多坑。 不妨设四个数字分别为<span class="math inline">\(a,b,c,d\)</span> 对<span class="math inline">\(a\)</span>和<span class="math inline">\(d\)</span>来说，我们可以直接知道数字0和数字1的个数。如果数字0的个数为<span class="math inline">\(p\)</span>，那么显然存在<span class="math display">\[\frac{p \times (p-1)}{2}=a\]</span> 于是对于<span class="math inline">\(a\)</span>和<span class="math inline">\(d\)</span>，我们只需要知道存不存在对应的解就行了。（注意：为0的时候有多个解....这个很坑） 然后我们可以解出0的个数<span class="math inline">\(ans0\)</span>和1的个数<span class="math inline">\(ans1\)</span>，因为这个东西是正反结合的，所以一个合法情况一定存在<span class="math inline">\(ans1 \times ans0 = b \times c\)</span> 我们只需要贪心满足<span class="math inline">\(b\)</span>就可以的，剩下的<span class="math inline">\(d\)</span>一定可以满足。</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;bits/stdc++.h&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> a,b,c,d;</span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> z,y;</span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> ans0,ans1;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	cin&gt;&gt;a&gt;&gt;b&gt;&gt;c&gt;&gt;d;</span><br><span class="line">	<span class="keyword">if</span>(b==<span class="number">0</span>&amp;&amp;c==<span class="number">0</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">if</span>(a==<span class="number">0</span>&amp;&amp;d==<span class="number">0</span>)</span><br><span class="line">		&#123;</span><br><span class="line">			ans0=<span class="number">1</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">else</span></span><br><span class="line">		&#123;</span><br><span class="line">			<span class="keyword">if</span>(a!=<span class="number">0</span>)</span><br><span class="line">			&#123;</span><br><span class="line">				a=a*<span class="number">2</span>;</span><br><span class="line">				z=<span class="built_in">sqrt</span>(a);</span><br><span class="line">				y=a/z;</span><br><span class="line">				<span class="keyword">if</span>(z*y!=a)</span><br><span class="line">				&#123;</span><br><span class="line">					cout&lt;&lt;<span class="string">&quot;Impossible&quot;</span>;</span><br><span class="line">					<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">				&#125;</span><br><span class="line">				<span class="keyword">if</span>(z+<span class="number">1</span>!=y)</span><br><span class="line">				&#123;</span><br><span class="line">					cout&lt;&lt;<span class="string">&quot;Impossible&quot;</span>;</span><br><span class="line">					<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">				&#125;</span><br><span class="line">				ans0=y;</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="keyword">if</span>(d!=<span class="number">0</span>)</span><br><span class="line">			&#123;</span><br><span class="line">				d=d*<span class="number">2</span>;</span><br><span class="line">				z=<span class="built_in">sqrt</span>(d);</span><br><span class="line">				<span class="keyword">if</span>(z==<span class="number">0</span>)</span><br><span class="line">				&#123;</span><br><span class="line">					cout&lt;&lt;<span class="string">&quot;Impossible&quot;</span>;</span><br><span class="line">					<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">				&#125;</span><br><span class="line">				y=d/z;</span><br><span class="line">				<span class="keyword">if</span>(z*y!=d)</span><br><span class="line">				&#123;</span><br><span class="line">					cout&lt;&lt;<span class="string">&quot;Impossible&quot;</span>;</span><br><span class="line">					<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">				&#125;</span><br><span class="line">				<span class="keyword">if</span>(z+<span class="number">1</span>!=y)</span><br><span class="line">				&#123;</span><br><span class="line">					cout&lt;&lt;<span class="string">&quot;Impossible&quot;</span>;</span><br><span class="line">					<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">				&#125;</span><br><span class="line">				ans1=y;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">	&#123;</span><br><span class="line">		a=a*<span class="number">2</span>;</span><br><span class="line">		z=<span class="built_in">sqrt</span>(a);</span><br><span class="line">		<span class="keyword">if</span>(z!=<span class="number">0</span>) y=a/z;</span><br><span class="line">		<span class="keyword">else</span> y=<span class="number">1</span>;</span><br><span class="line">		<span class="keyword">if</span>(z*y!=a)</span><br><span class="line">		&#123;</span><br><span class="line">			cout&lt;&lt;<span class="string">&quot;Impossible&quot;</span>;</span><br><span class="line">			<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">if</span>(z+<span class="number">1</span>!=y)</span><br><span class="line">		&#123;</span><br><span class="line">			cout&lt;&lt;<span class="string">&quot;Impossible&quot;</span>;</span><br><span class="line">			<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		ans0=y;</span><br><span class="line">		d=d*<span class="number">2</span>;</span><br><span class="line">		z=<span class="built_in">sqrt</span>(d);</span><br><span class="line">		<span class="keyword">if</span>(z!=<span class="number">0</span>) y=d/z;</span><br><span class="line">		<span class="keyword">else</span> y=<span class="number">1</span>;</span><br><span class="line">		<span class="keyword">if</span>(z*y!=d)</span><br><span class="line">		&#123;</span><br><span class="line">			cout&lt;&lt;<span class="string">&quot;Impossible&quot;</span>;</span><br><span class="line">			<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">if</span>(z+<span class="number">1</span>!=y)</span><br><span class="line">		&#123;</span><br><span class="line">			cout&lt;&lt;<span class="string">&quot;Impossible&quot;</span>;</span><br><span class="line">			<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		ans1=y;</span><br><span class="line">	&#125;</span><br><span class="line">	z=b+c;</span><br><span class="line">	y=ans0*ans1;</span><br><span class="line">	<span class="keyword">if</span>(z!=y)</span><br><span class="line">	&#123;</span><br><span class="line">		cout&lt;&lt;<span class="string">&quot;Impossible&quot;</span>;</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">long</span> <span class="keyword">long</span> l;</span><br><span class="line">	l=ans1;</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=ans0;i++)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">while</span>(l&gt;b) cout&lt;&lt;<span class="string">&quot;1&quot;</span>,l--;</span><br><span class="line">		cout&lt;&lt;<span class="string">&quot;0&quot;</span>;</span><br><span class="line">		b-=l;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">while</span>(l!=<span class="number">0</span>) cout&lt;&lt;<span class="string">&quot;1&quot;</span>,l--;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><a target="_blank" rel="noopener" href="http://codeforces.com/contest/708/problem/C">http://codeforces.com/contest/708/problem/C</a></p>
<blockquote>
<p>题目大意： 给定一棵树，求每个顶点，可不可以经过至多一次修改边的操作（修改之后要保证仍然是一棵树），成为重心（删除该点，剩余每个联通块的大小不超过<span class="math inline">\(\frac{n}{2}\)</span>）</p>
</blockquote>
<p>树形动态规划。 跟求树的重心那个dp差不多，首先选定一个点为根，对于每个点，维护向下的子树大小，还需要维护每个点向下的，可以删除的最大合法子树的大小（大小不超过<span class="math inline">\(\frac{n}{2}\)</span>），这样我们可以判断一个子树是否可以在不修改或者一次修改操作之后变得合法。 问题就在于向上的那个子树，对于这个子树，我们可以用<span class="math inline">\(n-size_i\)</span>，来计算从<span class="math inline">\(i\)</span>向上的子树的大小，对于删除的最大合法子树，用一次dfs，递归的找父亲节点的其他儿子和父亲节点的向上子树的对应值，之后的判断就很简单了，就判断一下通过一次操作，所有子树能不能合法就行。</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;bits/stdc++.h&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="keyword">int</span> n;</span><br><span class="line">vector&lt;<span class="keyword">int</span>&gt; e[<span class="number">400010</span>];</span><br><span class="line"><span class="keyword">int</span> pre[<span class="number">400010</span>]=&#123;&#125;;</span><br><span class="line"><span class="keyword">int</span> size[<span class="number">400010</span>]=&#123;&#125;;</span><br><span class="line"><span class="keyword">int</span> uu[<span class="number">400010</span>]=&#123;&#125;;</span><br><span class="line"><span class="keyword">int</span> up[<span class="number">400010</span>]=&#123;&#125;;</span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">cmp</span><span class="params">(pair&lt;<span class="keyword">int</span>,<span class="keyword">int</span>&gt; x,pair&lt;<span class="keyword">int</span>,<span class="keyword">int</span>&gt; y)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">return</span> x&gt;y;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function">pair&lt;<span class="keyword">int</span>,<span class="keyword">int</span>&gt; <span class="title">ff</span><span class="params">(<span class="keyword">int</span> pr,<span class="keyword">int</span> x)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">int</span> ans=<span class="number">0</span>;</span><br><span class="line">	<span class="keyword">int</span> u=<span class="number">0</span>;</span><br><span class="line">	pre[x]=pr;</span><br><span class="line">	pair&lt;<span class="keyword">int</span>,<span class="keyword">int</span>&gt; p;</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;e[x].<span class="built_in">size</span>();i++)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">if</span>(e[x][i]!=pr)</span><br><span class="line">		&#123;</span><br><span class="line">			p=<span class="built_in">ff</span>(x,e[x][i]);</span><br><span class="line">			u=<span class="built_in">max</span>(u,p.second);</span><br><span class="line">			ans+=p.first;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	ans++;</span><br><span class="line">	size[x]=ans;</span><br><span class="line">	<span class="keyword">if</span>(ans&lt;=n/<span class="number">2</span>) u=<span class="built_in">max</span>(ans,u);</span><br><span class="line">	uu[x]=u;</span><br><span class="line">	<span class="keyword">return</span> <span class="built_in">make_pair</span>(ans,u);</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">pan</span><span class="params">(<span class="keyword">int</span> x)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	vector&lt;pair&lt;<span class="keyword">int</span>,<span class="keyword">int</span>&gt; &gt; w;</span><br><span class="line">	w.<span class="built_in">push_back</span>(<span class="built_in">make_pair</span>(up[x],x));</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;e[x].<span class="built_in">size</span>();i++)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">if</span>(e[x][i]!=pre[x])</span><br><span class="line">		&#123;</span><br><span class="line">			w.<span class="built_in">push_back</span>(<span class="built_in">make_pair</span>(uu[e[x][i]],e[x][i]));</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="built_in">sort</span>(w.<span class="built_in">begin</span>(),w.<span class="built_in">end</span>(),cmp);</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;e[x].<span class="built_in">size</span>();i++)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">if</span>(e[x][i]!=pre[x])</span><br><span class="line">		&#123;</span><br><span class="line">			<span class="keyword">if</span>(w[<span class="number">0</span>].second==e[x][i]) up[e[x][i]]=<span class="built_in">max</span>(up[e[x][i]],w[<span class="number">1</span>].first);</span><br><span class="line">			<span class="keyword">else</span>  up[e[x][i]]=<span class="built_in">max</span>(up[e[x][i]],w[<span class="number">0</span>].first);</span><br><span class="line">			<span class="keyword">if</span>(n-size[e[x][i]]&lt;=n/<span class="number">2</span>) up[e[x][i]]=<span class="built_in">max</span>(up[e[x][i]],n-size[e[x][i]]);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;e[x].<span class="built_in">size</span>();i++)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">if</span>(e[x][i]!=pre[x])</span><br><span class="line">		&#123;</span><br><span class="line">			<span class="built_in">pan</span>(e[x][i]);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>,&amp;n);</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;n;i++)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">int</span> a,b;</span><br><span class="line">		<span class="built_in">scanf</span>(<span class="string">&quot;%d%d&quot;</span>,&amp;a,&amp;b);</span><br><span class="line">		e[a].<span class="built_in">push_back</span>(b);</span><br><span class="line">		e[b].<span class="built_in">push_back</span>(a);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="built_in">ff</span>(<span class="number">0</span>,<span class="number">1</span>);</span><br><span class="line">	up[<span class="number">1</span>]=<span class="number">0</span>;</span><br><span class="line">	<span class="built_in">pan</span>(<span class="number">1</span>);</span><br><span class="line">	<span class="comment">/*for(int i=1;i&lt;=n;i++)</span></span><br><span class="line"><span class="comment">	&#123;</span></span><br><span class="line"><span class="comment">		cout&lt;&lt;size[i]&lt;&lt;&quot; &quot;&lt;&lt;uu[i]&lt;&lt;&quot; &quot;&lt;&lt;up[i]&lt;&lt;endl;</span></span><br><span class="line"><span class="comment">	&#125;*/</span></span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=n;i++)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">bool</span> bj=<span class="number">0</span>;</span><br><span class="line">		<span class="keyword">bool</span> tt=<span class="number">0</span>;</span><br><span class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">0</span>;j&lt;e[i].<span class="built_in">size</span>();j++)</span><br><span class="line">		&#123;</span><br><span class="line">			<span class="keyword">if</span>(e[i][j]!=pre[i])</span><br><span class="line">			&#123;</span><br><span class="line">				<span class="keyword">if</span>(size[e[i][j]]&gt;n/<span class="number">2</span>&amp;&amp;(size[e[i][j]]-uu[e[i][j]])&lt;=n/<span class="number">2</span>&amp;&amp;bj==<span class="number">0</span>)</span><br><span class="line">				&#123;</span><br><span class="line">					bj=<span class="number">1</span>;</span><br><span class="line">				&#125;</span><br><span class="line">				<span class="keyword">else</span> <span class="keyword">if</span>(size[e[i][j]]&gt;n/<span class="number">2</span>)</span><br><span class="line">				&#123;</span><br><span class="line">					cout&lt;&lt;<span class="string">&quot;0 &quot;</span>;</span><br><span class="line">					tt=<span class="number">1</span>;</span><br><span class="line">					<span class="keyword">break</span>;</span><br><span class="line">				&#125;</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="keyword">else</span></span><br><span class="line">			&#123;</span><br><span class="line">				<span class="keyword">if</span>(n-size[i]&gt;n/<span class="number">2</span>&amp;&amp;(n-size[i]-up[i])&lt;=n/<span class="number">2</span>&amp;&amp;bj==<span class="number">0</span>)</span><br><span class="line">				&#123;</span><br><span class="line">					bj=<span class="number">1</span>;</span><br><span class="line">				&#125;</span><br><span class="line">				<span class="keyword">else</span> <span class="keyword">if</span>(n-size[i]&gt;n/<span class="number">2</span>)</span><br><span class="line">				&#123;</span><br><span class="line">					cout&lt;&lt;<span class="string">&quot;0 &quot;</span>;</span><br><span class="line">					tt=<span class="number">1</span>;</span><br><span class="line">					<span class="keyword">break</span>;</span><br><span class="line">				&#125;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">if</span>(tt==<span class="number">0</span>) cout&lt;&lt;<span class="string">&quot;1 &quot;</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><a target="_blank" rel="noopener" href="http://codeforces.com/contest/708/problem/D">http://codeforces.com/contest/708/problem/D</a></p>
<blockquote>
<p>题目大意： 给定一个网络流流量图（不一定合法），可以对每个边的流量和流量上限进行修改，使这个图合法，修改代价是所有边修改后的值和修改前的值的差的绝对值的和，求最小的代价,使这个图合法，就是让每个点的流量平衡（流入等于流出)</p>
</blockquote>
<p>直接搬运叉姐题解...</p>
<figure>
<img src="http://www.reku.me/wp-content/uploads/2016/08/捕获.png" alt="捕获"><figcaption aria-hidden="true">捕获</figcaption>
</figure>
<p>这里解释一下为什么n到1要连一条流量为无穷费用为0的边，因为点1和点n的流量本身是不平衡的，两个一起的流量应该是平衡的，所以应该加一条边平衡他俩的流量。</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;bits/stdc++.h&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> inn=<span class="number">1000000007</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Network</span>&#123;</span></span><br><span class="line"><span class="keyword">public</span>:   </span><br><span class="line">	<span class="keyword">typedef</span> <span class="keyword">long</span> <span class="keyword">long</span> VAL;</span><br><span class="line">	<span class="keyword">static</span> <span class="keyword">const</span> <span class="keyword">int</span> SIZE=<span class="number">1005</span>;</span><br><span class="line">	<span class="keyword">static</span> <span class="keyword">const</span> <span class="keyword">int</span> INF=<span class="number">1000000007</span>;</span><br><span class="line">	<span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span> <span class="title">ARC</span>&#123;</span></span><br><span class="line">		<span class="keyword">int</span> t,c;</span><br><span class="line">		VAL w;</span><br><span class="line">		ARC *o;</span><br><span class="line">	&#125; *PTR;</span><br><span class="line">	ARC arc[<span class="number">200005</span>];</span><br><span class="line">	PTR now[SIZE],e[SIZE];</span><br><span class="line">	<span class="keyword">int</span> cnt[SIZE],l[SIZE],r[SIZE],edge;</span><br><span class="line">	VAL sum;</span><br><span class="line">	<span class="function"><span class="keyword">void</span> <span class="title">clear</span><span class="params">()</span></span>&#123;</span><br><span class="line">		<span class="built_in">memset</span>(e,edge=sum=<span class="number">0</span>,<span class="built_in"><span class="keyword">sizeof</span></span>(e));</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function">ARC &amp;<span class="title">REV</span><span class="params">(PTR x)</span></span>&#123;</span><br><span class="line">		<span class="keyword">return</span> arc[(x-arc)^<span class="number">1</span>];</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">int</span> <span class="title">flow</span><span class="params">(<span class="keyword">int</span> S,<span class="keyword">int</span> T)</span></span>&#123;</span><br><span class="line">		<span class="keyword">return</span> <span class="built_in">spfa_johnson</span>(S,T,INF);</span><br><span class="line">	&#125; </span><br><span class="line">	<span class="function">PTR <span class="title">add_edge</span><span class="params">(<span class="keyword">int</span> x,<span class="keyword">int</span> y,<span class="keyword">int</span> c,VAL w=<span class="number">0</span>)</span></span>&#123;</span><br><span class="line">		e[x]=&amp;(arc[edge++]=(ARC)&#123;</span><br><span class="line">			y,c,+w,e[x]</span><br><span class="line">		&#125;);</span><br><span class="line">		e[y]=&amp;(arc[edge++]=(ARC)&#123;</span><br><span class="line">			x,<span class="number">0</span>,-w,e[y]</span><br><span class="line">		&#125;);</span><br><span class="line">		<span class="keyword">return</span> e[x];</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">int</span> <span class="title">aug</span><span class="params">(<span class="keyword">int</span> S,<span class="keyword">int</span> T,<span class="keyword">int</span> &amp;can)</span></span>&#123;</span><br><span class="line">		<span class="keyword">int</span> x,z=T,use=can;</span><br><span class="line">		<span class="keyword">for</span>(x=S;x!=T;x=now[x]-&gt;t) <span class="keyword">if</span>(use&gt;now[x]-&gt;c) use=now[z=x]-&gt;c;</span><br><span class="line">		<span class="keyword">for</span>(x=S;x!=T;x=now[x]-&gt;t)&#123;</span><br><span class="line">			now[x]-&gt;c-=use;</span><br><span class="line">			<span class="built_in">REV</span>(now[x]).c+=use;</span><br><span class="line">			sum+=use*now[x]-&gt;w;</span><br><span class="line">		&#125;</span><br><span class="line">		can-=use;</span><br><span class="line">		<span class="keyword">return</span> z;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">int</span> <span class="title">spfa_johnson</span><span class="params">(<span class="keyword">int</span> S,<span class="keyword">int</span> T,<span class="keyword">int</span> can)</span></span>&#123;</span><br><span class="line">		<span class="keyword">if</span>(S==T) <span class="keyword">return</span> can;</span><br><span class="line">		<span class="keyword">int</span> in=can,x,m;</span><br><span class="line">		VAL phi[SIZE],len[SIZE],MAXW=<span class="number">1000000007</span>;</span><br><span class="line">		<span class="built_in">memset</span>(l,<span class="number">0</span>,<span class="built_in"><span class="keyword">sizeof</span></span>(l));</span><br><span class="line">		<span class="built_in">fill_n</span>(phi,SIZE,MAXW);</span><br><span class="line">		phi[r[<span class="number">0</span>]=T]=<span class="number">0</span>;</span><br><span class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> i=m=<span class="number">0</span>;i&lt;=m;i++)</span><br><span class="line">		&#123;</span><br><span class="line">			l[x=r[i%SIZE]]=<span class="number">0</span>;</span><br><span class="line">			<span class="keyword">for</span>(PTR u=e[x];u;u=u-&gt;o)&#123;</span><br><span class="line">				<span class="keyword">if</span>(phi[x]+<span class="built_in">REV</span>(u).w&gt;=phi[u-&gt;t]||!<span class="built_in">REV</span>(u).c) <span class="keyword">continue</span>;</span><br><span class="line">				phi[u-&gt;t]=phi[x]+<span class="built_in">REV</span>(u).w;</span><br><span class="line">				<span class="keyword">if</span>(!l[u-&gt;t]) l[r[++m%SIZE]=u-&gt;t]=<span class="number">1</span>;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">do</span>&#123;</span><br><span class="line">			<span class="keyword">typedef</span> pair&lt;VAL,<span class="keyword">int</span>&gt; TPL;</span><br><span class="line">			priority_queue&lt;TPL&gt; q;</span><br><span class="line">			<span class="built_in">fill_n</span>(len,SIZE,MAXW);</span><br><span class="line">			<span class="built_in">memset</span>(l,<span class="number">0</span>,<span class="built_in"><span class="keyword">sizeof</span></span>(l));</span><br><span class="line">			q.<span class="built_in">push</span>(<span class="built_in">TPL</span>(len[T]=<span class="number">0</span>,T));</span><br><span class="line">			<span class="keyword">while</span>(q.<span class="built_in">size</span>())&#123;</span><br><span class="line">				x=q.<span class="built_in">top</span>().second;</span><br><span class="line">				q.<span class="built_in">pop</span>();</span><br><span class="line">				<span class="keyword">if</span>(!l[x]) l[x]=<span class="number">1</span>;</span><br><span class="line">				<span class="keyword">else</span> <span class="keyword">continue</span>;</span><br><span class="line">				<span class="keyword">for</span>(PTR u=e[x];u;u=u-&gt;o)&#123;</span><br><span class="line">					<span class="keyword">if</span>(!<span class="built_in">REV</span>(u).c||l[u-&gt;t]) <span class="keyword">continue</span>;</span><br><span class="line">					VAL at=len[x]+phi[x]+<span class="built_in">REV</span>(u).w-phi[u-&gt;t];</span><br><span class="line">					<span class="keyword">if</span>(at&gt;=len[u-&gt;t]) <span class="keyword">continue</span>;</span><br><span class="line">					len[u-&gt;t]=at;</span><br><span class="line">					now[u-&gt;t]=&amp;<span class="built_in">REV</span>(u);</span><br><span class="line">					q.<span class="built_in">push</span>(<span class="built_in">TPL</span>(-at,u-&gt;t));</span><br><span class="line">				&#125; </span><br><span class="line">			&#125;</span><br><span class="line">			<span class="keyword">for</span>(x=<span class="number">0</span>;x&lt;SIZE;x++) phi[x]+=len[x];</span><br><span class="line">		&#125;<span class="keyword">while</span>(phi[S]&lt;MAXW&amp;&amp;<span class="built_in">aug</span>(S,T,can)!=T);</span><br><span class="line">		<span class="keyword">return</span> in-can;</span><br><span class="line">	&#125;</span><br><span class="line">&#125; net;</span><br><span class="line"><span class="keyword">int</span> n,m;</span><br><span class="line"><span class="keyword">long</span> <span class="keyword">long</span> ans=<span class="number">0</span>;</span><br><span class="line"><span class="keyword">int</span> u,v,c,f;</span><br><span class="line"><span class="keyword">int</span> fei[<span class="number">110</span>]=&#123;&#125;;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%d%d&quot;</span>,&amp;n,&amp;m);</span><br><span class="line">	net.<span class="built_in">clear</span>();</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=m;i++)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">scanf</span>(<span class="string">&quot;%d%d%d%d&quot;</span>,&amp;u,&amp;v,&amp;c,&amp;f);</span><br><span class="line">		fei[u]-=f;</span><br><span class="line">		fei[v]+=f;</span><br><span class="line">		<span class="keyword">if</span>(f&lt;=c)</span><br><span class="line">		&#123;</span><br><span class="line">			net.<span class="built_in">add_edge</span>(u,v,c-f,<span class="number">1</span>);</span><br><span class="line">			net.<span class="built_in">add_edge</span>(u,v,inn,<span class="number">2</span>);</span><br><span class="line">			net.<span class="built_in">add_edge</span>(v,u,f,<span class="number">1</span>);</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">else</span></span><br><span class="line">		&#123;</span><br><span class="line">			ans+=f-c;</span><br><span class="line">			net.<span class="built_in">add_edge</span>(v,u,f-c,<span class="number">0</span>);</span><br><span class="line">			net.<span class="built_in">add_edge</span>(u,v,inn,<span class="number">2</span>);</span><br><span class="line">			net.<span class="built_in">add_edge</span>(v,u,c,<span class="number">1</span>);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	net.<span class="built_in">add_edge</span>(n,<span class="number">1</span>,inn,<span class="number">0</span>);</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=n;i++)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">if</span>(fei[i]&gt;<span class="number">0</span>)</span><br><span class="line">		&#123;</span><br><span class="line">			net.<span class="built_in">add_edge</span>(n+<span class="number">1</span>,i,fei[i],<span class="number">0</span>);</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">else</span> net.<span class="built_in">add_edge</span>(i,n+<span class="number">2</span>,-fei[i],<span class="number">0</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	net.<span class="built_in">flow</span>(n+<span class="number">1</span>,n+<span class="number">2</span>);</span><br><span class="line">	cout&lt;&lt;ans+net.sum;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/%E7%BD%91%E7%BB%9C%E6%B5%81/" rel="tag"># 网络流</a>
              <a href="/tags/codeforces/" rel="tag"># codeforces</a>
              <a href="/tags/%E6%9C%80%E5%B0%8F%E8%B4%B9%E7%94%A8%E6%9C%80%E5%A4%A7%E6%B5%81/" rel="tag"># 最小费用最大流</a>
              <a href="/tags/%E6%9E%84%E9%80%A0/" rel="tag"># 构造</a>
              <a href="/tags/%E6%A0%91DP/" rel="tag"># 树DP</a>
              <a href="/tags/%E8%B4%AA%E5%BF%83/" rel="tag"># 贪心</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2016/08/21/%E4%B8%89%E5%88%86%E6%9F%A5%E6%89%BE/" rel="prev" title="模板：三分查找">
      <i class="fa fa-chevron-left"></i> 模板：三分查找
    </a></div>
      <div class="post-nav-item">
    <a href="/2016/08/30/educational-codeforces-round-7-f-the-sum-of-the-k-th-powers/" rel="next" title="Educational Codeforces Round 7  F. The Sum of the k-th Powers">
      Educational Codeforces Round 7  F. The Sum of the k-th Powers <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          
    <div class="comments" id="valine-comments"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">Reku</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">78</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
        <span class="site-state-item-count">8</span>
        <span class="site-state-item-name">categories</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">96</span>
        <span class="site-state-item-name">tags</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/wyc-ruiker" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;wyc-ruiker" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://www.zhihu.com/people/reku1997" title="ZhiHu → https:&#x2F;&#x2F;www.zhihu.com&#x2F;people&#x2F;reku1997" rel="noopener" target="_blank"><i class="fa fa-fw fa-quora"></i>ZhiHu</a>
      </span>
      <span class="links-of-author-item">
        <a href="http://codeforces.com/profile/reku" title="CodeForces → http:&#x2F;&#x2F;codeforces.com&#x2F;profile&#x2F;reku" rel="noopener" target="_blank"><i class="fa fa-fw fa-code"></i>CodeForces</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://www.linkedin.cn/injobs/in/reku" title="Linkedin → https:&#x2F;&#x2F;www.linkedin.cn&#x2F;injobs&#x2F;in&#x2F;reku" rel="noopener" target="_blank"><i class="fa fa-fw fa-linkedin"></i>Linkedin</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://gitee.com/reku1997" title="Gitee → https:&#x2F;&#x2F;gitee.com&#x2F;reku1997" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i>Gitee</a>
      </span>
      <span class="links-of-author-item">
        <a href="/./atom.xml" title="RSS → .&#x2F;atom.xml"><i class="fa fa-fw fa-rss"></i>RSS</a>
      </span>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 2016 – 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Reku</span>
</div>
  <div class="powered-by">Powered by <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a>
  </div>

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="Total Visitors">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="Total Views">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  




  
<script src="//cdn.jsdelivr.net/npm/algoliasearch@4/dist/algoliasearch-lite.umd.js"></script>
<script src="//cdn.jsdelivr.net/npm/instantsearch.js@4/dist/instantsearch.production.min.js"></script>
<script src="/js/algolia-search.js"></script>














  

  
      

<script>
  if (typeof MathJax === 'undefined') {
    window.MathJax = {
      loader: {
        source: {
          '[tex]/amsCd': '[tex]/amscd',
          '[tex]/AMScd': '[tex]/amscd'
        }
      },
      tex: {
        inlineMath: {'[+]': [['$', '$']]},
        tags: 'ams'
      },
      options: {
        renderActions: {
          findScript: [10, doc => {
            document.querySelectorAll('script[type^="math/tex"]').forEach(node => {
              const display = !!node.type.match(/; *mode=display/);
              const math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display);
              const text = document.createTextNode('');
              node.parentNode.replaceChild(text, node);
              math.start = {node: text, delim: '', n: 0};
              math.end = {node: text, delim: '', n: 0};
              doc.math.push(math);
            });
          }, '', false],
          insertedScript: [200, () => {
            document.querySelectorAll('mjx-container').forEach(node => {
              let target = node.parentNode;
              if (target.nodeName.toLowerCase() === 'li') {
                target.parentNode.classList.add('has-jax');
              }
            });
          }, '', false]
        }
      }
    };
    (function () {
      var script = document.createElement('script');
      script.src = '//cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js';
      script.defer = true;
      document.head.appendChild(script);
    })();
  } else {
    MathJax.startup.document.state(0);
    MathJax.texReset();
    MathJax.typeset();
  }
</script>

    

  


<script>
NexT.utils.loadComments(document.querySelector('#valine-comments'), () => {
  NexT.utils.getScript('//unpkg.com/valine/dist/Valine.min.js', () => {
    var GUEST = ['nick', 'mail', 'link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item => {
      return GUEST.includes(item);
    });
    new Valine({
      el         : '#valine-comments',
      verify     : false,
      notify     : false,
      appId      : 'MWLzM550UOu69h3dgvbbLSsF-gzGzoHsz',
      appKey     : 'gkKnwm9FK0cu3ysJbcggsCDz',
      placeholder: "Just go go",
      avatar     : 'mm',
      meta       : guest,
      pageSize   : '10' || 10,
      visitor    : true,
      lang       : '' || 'zh-cn',
      path       : location.pathname,
      recordIP   : false,
      serverURLs : ''
    });
  }, window.Valine);
});
</script>

</body>
</html>
